<?php
/**
 * Created by PhpStorm.
 * User: tengjs
 * Date: 2020/11/3
 * Time: 7:37
 */

namespace App\Http\Controllers\Api\Publication;

use App\Admin\Models\TimelineConfigModel;
use App\Http\Controllers\Api\BaseController;
use Illuminate\Support\Facades\Validator;
use App\Admin\Models\ContentModel;

class PublicationController extends BaseController
{
    /**
     * publication
     *
     * @return array
     */
    public function groupList()
    {
        $contentType = 13;
        $file = [
            'content_id', 'content_date', 'title_en as title', 'presenter_en as author', 'address_en as journals',
            'period_en as impact_factor'
        ];
        $list = ContentModel::select($file)->where('content_type', '=', $contentType)
            ->orderBy('content_date', 'desc')
            ->orderBy('order', 'asc')
            ->get()->toArray();

        // 将list按content_date分组，组内按order顺序排列
        $groupList = [];
        if (!empty($list)) {
            foreach ($list as $item) {
                $date = $item['content_date'];
                if (!isset($groupList[$date])) {
                    $groupList[$date] = [];
                }
                $groupList[$date][] = $item;
            }
        }

        $data = [];
        foreach ($groupList as $date => $item) {
            $data[] = [
                'date' => $date,
                'list' => $item
            ];
        }

        // 数据查询
        $result = [
            'code' => 0,
            'msg' => '数据获取成功！',
            'data' => $data
        ];

        return $this->success($result['data'], $result['msg'] ?: '数据获取成功！');
    }
}
